package top.haijunit.work.modules.core.enums;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatusEnum implements IEnum<Integer> {


    /**
     * 有效的
     */
    VALID(1, "有效的"),
    /**
     * 未激活
     */
    PADDING(2, "未激活"),
    /**
     * 已锁定
     */
    LOCK(3, "已锁定"),
    /**
     * 已停用
     */
    DISUSE(4, "已停用"),
    ;

    @Schema(description = "状态")
    private final Integer value;
    @Schema(description = "描述")
    private final String message;

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static StatusEnum getInstance(String name) {
        StatusEnum statusEnum = EnumUtil.fromStringQuietly(StatusEnum.class, name);
        if (null != statusEnum) {
            return statusEnum;
        }
        for (StatusEnum item : StatusEnum.values()) {
            if (CharSequenceUtil.isNumeric(name)) {
                if (item.getValue() == Integer.parseInt(name)) {
                    return item;
                }
            } else {
                if (item.name().equals(name)) {
                    return item;
                }
            }
        }
        return null;
    }
}
